package com.yuyou.tas.gateway.fallback;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;

import org.springframework.cloud.netflix.zuul.filters.route.ZuulFallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component;

import com.yuyou.tas.util.bean.ReturnCode;
import com.yuyou.tas.util.bean.ReturnResult;

@Component  
public class LessonServiceFallbackProvider implements ZuulFallbackProvider {  
  
    @Override  
    public String getRoute() {  
        return "tas-lesson-service";  
    }  
  
    @Override  
    public ClientHttpResponse fallbackResponse() {  
        return new ClientHttpResponse() {  
            @Override  
            public HttpStatus getStatusCode() throws IOException {  
                return HttpStatus.OK;  
            }  
  
            @Override  
            public int getRawStatusCode() throws IOException {  
                return this.getStatusCode().value();  
            }  
  
            @Override  
            public String getStatusText() throws IOException {  
                return this.getStatusCode().getReasonPhrase();  
            }  
  
            @Override  
            public void close() {  
  
            }  
  
            @Override  
            public InputStream getBody() throws IOException {  
//            	ReturnResult<String> res=new ReturnResult<String>(false,ReturnCode.code._2002.getMessage()+":服务异常,请稍后再试",null,ReturnCode.code._2002.getCode(),ReturnCode.code._2002.getMessage()+":tas-lesson-service不可用");
            	ReturnResult<String> res=new ReturnResult<String>(false,"服务繁忙,请稍后再试",null,ReturnCode.code._2002.getCode(),"服务繁忙,请稍后再试");
            	return new ByteArrayInputStream(res.toJsonString().getBytes("utf-8")); 
            }  
  
            @Override  
            public HttpHeaders getHeaders() {  
                HttpHeaders headers = new HttpHeaders();  
                MediaType mt = new MediaType("application", "json", Charset.forName("UTF-8"));  
                headers.setContentType(mt);  
                return headers;  
            }  
        };  
    }  
}  
